package com.example.demo1.controller;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/home")
public class HomeController {
    @GetMapping("/index")
    public String deviceType() {
        return "ind";
    }

    @GetMapping("/menu")
    public String menu() throws IOException {
        System.out.println("/menu");
        var in = this.getClass().getClassLoader().getResourceAsStream("static/menu.json");
        return getFileContent(in);
    }

    @GetMapping("/authProjectTree")
    public String authProjectTree() {
        System.out.println("/authProjectTree");
        return "[{\"id\":\"aiot\",\"code\":\"IOSV2024061710852\",\"name\":\"第二轮系统测试\",\"type\":1,\"typeName\":\"集团\",\"parentType\":null,\"parentTypeName\":null,\"projectId\":null,\"parentId\":\"\",\"parentName\":\"\",\"path\":\"aiot\",\"pathName\":\"第二轮系统测试\",\"optional\":true,\"leaf\":null,\"isParent\":null,\"children\":[{\"id\":\"1252185978959392768\",\"code\":\"IOStest1\",\"name\":\"系统测试项目一\",\"type\":7,\"typeName\":\"项目\",\"parentType\":1,\"parentTypeName\":\"集团\",\"projectId\":\"1252185978959392768\",\"parentId\":\"aiot\",\"parentName\":\"第二轮系统测试\",\"path\":\"aiot,1252185978959392768\",\"pathName\":\"第二轮系统测试/系统测试项目一\",\"optional\":true,\"leaf\":null,\"isParent\":null,\"children\":null},{\"id\":\"1252186070965645312\",\"code\":\"IOStest2\",\"name\":\"系统测试项目二\",\"type\":7,\"typeName\":\"项目\",\"parentType\":1,\"parentTypeName\":\"集团\",\"projectId\":\"1252186070965645312\",\"parentId\":\"aiot\",\"parentName\":\"第二轮系统测试\",\"path\":\"aiot,1252186070965645312\",\"pathName\":\"第二轮系统测试/系统测试项目二\",\"optional\":true,\"leaf\":null,\"isParent\":null,\"children\":null},{\"id\":\"1252186158324609024\",\"code\":\"IOStest3\",\"name\":\"系统测试项目三\",\"type\":7,\"typeName\":\"项目\",\"parentType\":1,\"parentTypeName\":\"集团\",\"projectId\":\"1252186158324609024\",\"parentId\":\"aiot\",\"parentName\":\"第二轮系统测试\",\"path\":\"aiot,1252186158324609024\",\"pathName\":\"第二轮系统测试/系统测试项目三\",\"optional\":true,\"leaf\":null,\"isParent\":null,\"children\":null}]}]";
    }

    //没有过期时间
    //accessToken:eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzZXJ2ZXIiOmZhbHNlLCJzdWIiOiIxNzc4NTk0ODEzNjM2NDE1NDg5IiwiZmxhZyI6MiwidXNlcl9uYW1lIjoidDEiLCJzY29wZSI6WyJzZXZlciJdLCJuYW1lIjoidDEiLCJqdGkiOiI5MDFjYmZlNi1mZGVjLTRiODYtYmM0Yy04YTI4M2U4NmM2YjQiLCJ0ZW5hbnQiOiJ0YWlvdCIsImNsaWVudF9pZCI6InN5cyJ9.tWdTJ-0t05w1Puck7xe160faS_uEP7q-FvQkTuISols

    // 过期时间5分钟
    //accessToken:eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzZXJ2ZXIiOmZhbHNlLCJzdWIiOiIxNzc4NTk0ODEzNjM2NDE1NDg5IiwiZmxhZyI6MiwidXNlcl9uYW1lIjoidDEiLCJzY29wZSI6WyJzZXZlciJdLCJuYW1lIjoidDEiLCJleHAiOjE3MTMyNTc2MDEsImp0aSI6ImI3MDQ5YjM2LWE3YWUtNDQwOC1hNDZmLTU1OTFiYmFkZThjMSIsInRlbmFudCI6InRhaW90IiwiY2xpZW50X2lkIjoic3lzIn0.jyk3yyQM8vIrIC3UCWhGtFvpgQ3_ZYDBYWIbd7yisEY

    private String getFileContent(Object fileInPath) throws IOException {
        BufferedReader br = null;
        if (fileInPath == null) {
            return null;
        }
        if (fileInPath instanceof String) {
            br = new BufferedReader(new FileReader(new File((String) fileInPath)));
        } else if (fileInPath instanceof InputStream) {
            br = new BufferedReader(new InputStreamReader((InputStream) fileInPath));
        }
        StringBuilder lines = new StringBuilder();
        String line;
        while ((line = br.readLine()) != null) {
            lines.append(line);
        }
        br.close();
        return lines.toString();
    }
}
